package atm.dao.impl;

import atm.dao.BaseDAO;
import atm.dao.UserDAO;
import atm.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//JUnit 单元测试

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getBYUsernameAndPwd(String username, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;

        String sql = "select * from t_bank where b_username=? and b_userpsw=? ";
        try {
            //2.获取执行SQL语句的对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            //3.执行SQL并返回结果集
            rs = statement.executeQuery();

            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        this.getConnection();
//        PreparedStatement statement = null;
        String sql = "update t_bank set b_userpsw=? , b_money=? where b_id=? ";
        try {
//            con.setAutoCommit(false);
            //2.获取执行SQL语句对象,此处为prepareStatement,要预编译
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPassword());
            statement.setObject(2, user.getMoney());
            statement.setObject(3, user.getId());
            //3.执行SQL语句
            int row = statement.executeUpdate();

//            con.commit();
//            System.out.println("影响的行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
//            try {
//                con.rollback();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
        } finally {
            //4.关闭连接
//            close(con, statement);
        }
    }

    @Override
    public UserDO getById(int id) {
        this.getConnection();
//        PreparedStatement statement = null;
//        ResultSet rs = null;
        UserDO user = null;

        String sql = "select * from t_bank where b_id=? ";
        try {
            //2.获取执行SQL语句的对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            //3.执行SQL并返回结果集
            rs = statement.executeQuery();

            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getByUsername(String username) {
        this.getConnection();
//        PreparedStatement statement = null;
//        ResultSet rs = null;
        UserDO user = null;

        String sql = "select * from t_bank where b_username=? ";
        try {
            //2.获取执行SQL语句的对象,此处为prepareStatement,要预编译
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            //3.执行SQL并返回结果集
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

}
